<?php
/**
 * Created by PhpStorm.
 * User: yidashi
 * Date: 16/7/24
 * Time: 下午4:22
 */

namespace common\helpers;


use yii\helpers\ArrayHelper;
use common\records\Toy;
use common\records\UserInfo;

class Util
{
    /**
     * 解析url 格式: route[空格,回车]a=1&b=2
     * @param $url
     * @return array
     */
    public static function parseUrl($url)
    {
        if (strpos($url, '//') !== false) {
            return $url;
        }
        // 空格换行都行
        $url = preg_split('/[ \r\n]+/', $url);
        if (isset($url[1])) {
            $tmp = $url[1];
            unset($url[1]);
            $tmpParams = explode('&', $tmp);
            $params = [];
            foreach ($tmpParams as $tmpParam) {
                list($key, $value) = explode('=', $tmpParam);
                $params[$key] = $value;
            }
            $url = array_merge($url, $params);
        }
        return $url;
    }

    public static function getEntityList()
    {
        return [
            'common\models\Suggest' => '留言',
            'common\models\Page' => '单页',
            'common\models\Article' => '文章',
            'common\modules\book\models\Book' => '书',
            'common\modules\book\models\BookChapter' => '书章节',
        ];
    }
    public static function getEntityName($entity)
    {
        $entityList = self::getEntityList();
        return ArrayHelper::getValue($entityList, $entity, $entity);
    }

    public static function getToyName($toy_id){
        $toys = Toy::find()->select("id, name")->where([])->asArray()->all();
        $toy_ids = array();
        foreach($toys AS $item)
        {   
            $toy_ids[$item['id']] = $item['name'];
        }
        return $toy_ids[$toy_id];
    }
    public static function getPowerName($power){
        $power_desp = array();
        $power_desp['10'] = "弱";
        $power_desp['20'] = "中";
        $power_desp['35'] = "强";

        return $power_desp[strval($power)];
    }
    public static function getUserName($uid){
        $user = UserInfo::find()->where(['uid'=>$uid])->one();        
        if($user == null)
            return "已不存在用户";
        return $user->nick;
    }
    public static function getUserPhoto($uid){
        $user = UserInfo::find()->where(['uid'=>$uid])->one();
        if($user == null)
            return "";
        return $user->photo;
    }
    public static function tranTime($strtime)
    {
        $time = strtotime($strtime);
        $rtime = date("Y-m-d H:i",$time);
        $htime = date("H:i",$time);

        $time = time() - $time;

        if ($time < 60)
        {
            $str = '刚刚';
        }
        elseif ($time < 60 * 60)
        {
            $min = floor($time/60);
            $str = $min.'分钟前';
        }
        elseif ($time < 60 * 60 * 24)
        {
            $h = floor($time/(60*60));
            $str = $h.'小时前';
        }
        elseif ($time < 60 * 60 * 24 * 3)
        {
            $d = floor($time/(60*60*24));
            if($d==1)
                $str = '昨天';
            else
                $str = '前天';
        }
        else
        {
            $str = $rtime;
        }
        return $str;
    }          
    public static function getRecordStatus($status)
    {
        $desp = array();
        $desp[0] = "未同步";
        $desp[1] = "抓到";
        $desp[2] = "未抓到";
        return $desp[$status];
    }
    public static function getPayStatus($status)
    {
        $desp = array();
        $desp[0] = "未支付";
        $desp[1] = "支付成功";
        $desp[2] = "支付失败";
        return $desp[$status];
    }
    public static function getOrderStatus($status)
    {
        $desp = array();
        $desp[0] = "未完成";
        $desp[1] = "已完成";
        $desp[2] = "已退币";
        return $desp[$status];
    }
    public static function getSex($sex)
    {
        $desp = array();
        $desp[1] = "男";
        $desp[2] = "女";
        return $desp[$sex];
    }
}
